package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.mapper.AdminMapper;
import com.hxzy.pojo.Admin;
import com.hxzy.service.AdminService;
import com.hxzy.util.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public boolean addAdmin(Admin admin) {
        String hashpw = BCrypt.hashpw(admin.getPassword(), BCrypt.gensalt());
        admin.setPassword(hashpw);
        int insert = adminMapper.insert(admin);
        if (insert > 0){
            return true;
        }
        return false;
    }

    public boolean login(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",admin.getName());
        Admin user = adminMapper.selectOne(wrapper);
        if (user == null){
            return false;
        }else {
            return BCrypt.checkpw(admin.getPassword(), user.getPassword());
        }
    }



}
